<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * Session 处理控制类
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
abstract class EGP_Session
{
    /**
     * 标识 session 是否已启动
     *
     * @var boolean
     */
    protected static $_started = false;

    /**
     * 标识 session 是否重新生成过 id.
     *
     * @var boolean
     */
    private static $_regenerated = false;

    /**
     * session 配置选项
     *
     * @var array
     */
    protected static $_options = array(
        'name' => null,
        'path' => null,
        'lifetime' => 86400,
        'domain' => null,
    );

    /**
     * 设定 session 选项
     *
     * @param  array  $options
     */
    public static function setOptions(array $options = array())
    {
        if (!empty($options) && is_array($options)) {
            foreach ($options as $k => $v) {
                self::$_options[$k] = trim($v);
            }
        }

//        $params = session_get_cookie_params();
//        session_set_cookie_params(
//            $params['lifetime'],
//            '/',
//            self::$_options['domain'],
//            $params['secure']
//        );
    }

    /**
     * 启动 session
     *
     */
    public static function start()
    {
        if (!self::$_started) {
            self::$_options['name'] && session_name(self::$_options['name']);
            self::$_options['path'] && session_save_path(self::$_options['path']);
            isset($_COOKIE[session_name()]) && session_id($_COOKIE[session_name()]);
            @session_cache_limiter('private, must-revalidate');
            @session_start();
            self::$_started = true;
        }
    }

    /**
     * 重新生成 session id
     *
     */
    public static function regenerateId()
    {
        if (self::$_started && !self::$_regenerated) {
            session_regenerate_id(true); // 重新生成 session id 并删除旧 session
            self::$_regenerated = true;
        } else {
            self::$_regenerated = false;
        }
    }

    /**
     * 设置 session id 的 cookie 存储时间
     *
     * @param  integer  $seconds  保存的秒数
     */
    public static function rememberUntil($seconds = 0)
    {
        setcookie(session_name(), session_id(), time() + $seconds, '/', self::$_options['domain']);
    }

    /**
     * 将 session id 存储到 cookie 中
     *
     * @param  integer  $seconds  保存的秒数
     */
    public static function rememberMe($seconds = 86400)
    {
        empty($seconds) && $seconds = self::$_options['lifetime'];
        $seconds > 0 && self::rememberUntil($seconds);
    }

    /**
     * 清除 cookie 中的 session id
     *
     */
    public static function forgetMe()
    {
        self::rememberUntil(-3600);
    }

    /**
     * 启用 session 命名空间
     *
     * @param  string  $namespace
     */
    public static function namespace($namespace = null)
    {
        return new EGP_Session_NameSpace($namespace);
    }

    /**
     * Sets user-level session storage functions
     *
     * @param EGP_Session_SaveHandler_Interface $saveHandler
     */
    public static function setSaveHandler(EGP_Session_SaveHandler_Interface $saveHandler)
    {
        session_set_save_handler(
            array(&$saveHandler, 'open'),
            array(&$saveHandler, 'close'),
            array(&$saveHandler, 'read'),
            array(&$saveHandler, 'write'),
            array(&$saveHandler, 'destroy'),
            array(&$saveHandler, 'gc')
        );
    }

}